//
//  LMTabBarController.m
//  李明微博
//
//  Created by 张青花 on 15/12/2.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMTabBarController.h"
#import "UIImage+Image.h"
#import "LMTabBar.h"

#import "LMHomeViewController.h"
#import "LMDiscoverViewController.h"
#import "LMMessageViewController.h"
#import "LMProfileViewController.h"
#import "LMNavigationController.h"
#import "LMTabBarButton.h"

#import "LMUserTool.h"

@interface LMTabBarController ()<LMTaBarDelegate>

@property(nonatomic,strong)NSMutableArray *items;

@property(nonatomic,weak)LMHomeViewController *home;

@property(nonatomic,weak)LMMessageViewController *message;

@property(nonatomic,weak)LMProfileViewController *profile;
@end

@implementation LMTabBarController

- (NSMutableArray *)items
{
    if (_items ==nil) {
        
        _items = [NSMutableArray array];
        
    }
    return _items;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupAddchildViewController];
    
    
    /*
     TabBar 下面的 TabBar 直接复制给LMTabBar 
     再TabBar中 甚至需要添加的新的UIview
     */

    [self setUpTabBar];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(requestUnread) userInfo:nil repeats:YES];
    
    
}


- (void)setUpTabBar
{
    LMTabBar *tabBar = [[LMTabBar alloc] initWithFrame:self.tabBar.bounds];
    
    tabBar.backgroundColor = [UIColor whiteColor];
    
    tabBar.delegate = self;
    
    tabBar.items = self.items;
    
    [self.tabBar addSubview:tabBar];
    
//    [self.tabBar removeFromSuperview];
    
}


- (void)requestUnread
{
    NSLog(@"%s",__func__);
    
    [LMUserTool unreadWithSucess:^(LMUserResult *status) {
        _home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",status.status];
        
        _message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",status.messageCount];
 
        _profile.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",status.follower];
        
        [UIApplication sharedApplication].applicationIconBadgeNumber = status.totoalCount;

        
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)tabBar:(LMTabBar *)tabBar didClickButton:(NSInteger)index
{
    if (index == 0 && self.selectedIndex ==index) {
        [_home refresh];
    }
    self.selectedIndex = index;
}

- (void)setupAddchildViewController
{
    LMHomeViewController *home = [[LMHomeViewController alloc] init];
    
    
    [self addChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"];
    
    home.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    LMMessageViewController *message = [[LMMessageViewController alloc] init];
    
    
    [self addChildViewController:message image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"];
    
    message.view.backgroundColor = [UIColor redColor];
    
    
    
    LMDiscoverViewController *discover = [[LMDiscoverViewController alloc] init];
    
    [self addChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"];
    
    discover.view.backgroundColor = [UIColor orangeColor];
    
    
    
    
    LMProfileViewController *profile = [[LMProfileViewController alloc] init];
    
     [self addChildViewController:profile image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"个人"];
    
     profile.view.backgroundColor = [UIColor purpleColor];
    
    
    
}

- (void)addChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title
{
//    vc.title = title;
//    vc.tabBarItem.image = image;
//    vc.tabBarItem.selectedImage = selectedImage;
//    
    

    vc.tabBarItem  = [[UITabBarItem alloc] initWithTitle:title image:image selectedImage:selectedImage];
    
    [self.items addObject:vc.tabBarItem];
    
    LMNavigationController *nav =[[LMNavigationController alloc] initWithRootViewController:vc];
    
    
    
    
    [self addChildViewController:nav];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
    
    
    
}



@end
